package com.lanou.renrenxin.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.tools.RenRenXinHelper;
import com.lanou.renrenxin.tools.RenRenXinModel;
import com.lanou.renrenxin.activity.BlackListActivity;
import com.lanou.renrenxin.activity.LoginActivity;
import com.lanou.renrenxin.activity.MainActivity;
import com.lanou.renrenxin.tools.EaseSwitchButton;

/**
 * Created by zy on 16/3/1.
 */
public class SetFragment extends Fragment implements View.OnClickListener {
    //退出按钮
    private Button btn_login_out;
    /**
     * 设置新消息通知布局
     */
    private LinearLayout ll_switch_notification;
    /**
     * 设置声音布局
     */
    private LinearLayout ll_switch_sound;
    /**
     * 设置震动布局
     */
    private LinearLayout ll_switch_shake;
    /**
     * 设置扬声器布局
     */
    private LinearLayout ll_switch_speaker;
    //黑名单布局
    private LinearLayout ll_blacklistContainer;

    private EaseSwitchButton switch_notification;
    private EaseSwitchButton switch_sound;
    private EaseSwitchButton switch_shake;
    private EaseSwitchButton switch_speaker;
    private RenRenXinModel settingsModel;

    private TextView tvUsername;
    private Context context;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;

    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fragment_set,container,false);
//        return view;
        return inflater.inflate(R.layout.fragment_set, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btn_login_out = (Button) getView().findViewById(R.id.login_out);

        ll_switch_notification = (LinearLayout) getView().findViewById(R.id.ll_switch_notification);
        ll_switch_sound = (LinearLayout) getView().findViewById(R.id.ll_switch_sound);
        ll_switch_shake = (LinearLayout) getView().findViewById(R.id.ll_switch_shake);
        ll_switch_speaker = (LinearLayout) getView().findViewById(R.id.ll_switch_speaker);
        ll_blacklistContainer = (LinearLayout) getView().findViewById(R.id.ll_black_list);

        switch_notification = (EaseSwitchButton) getView().findViewById(R.id.switch_notification);
        switch_sound = (EaseSwitchButton) getView().findViewById(R.id.switch_sound);
        switch_shake = (EaseSwitchButton) getView().findViewById(R.id.switch_shake);
        switch_speaker = (EaseSwitchButton) getView().findViewById(R.id.switch_speaker);

        btn_login_out.setOnClickListener(this);
        ll_switch_notification.setOnClickListener(this);
        ll_switch_sound.setOnClickListener(this);
        ll_switch_shake.setOnClickListener(this);
        ll_switch_speaker.setOnClickListener(this);
        ll_blacklistContainer.setOnClickListener(this);


        //个人信息昵称
        tvUsername = (TextView) getView().findViewById(R.id.personal_niCheng);
        tvUsername.setText(EMChatManager.getInstance().getCurrentUser());

        settingsModel = new RenRenXinModel(context);
        // 震动和声音总开关，来消息时，是否允许此开关打开
        if (settingsModel.getSettingMsgNotification()){
            switch_notification.openSwitch();
        } else {
            switch_notification.closeSwitch();
        }
        // 是否打开声音
        if (settingsModel.getSettingMsgSound()){
            switch_sound.openSwitch();
        } else {
            switch_sound.closeSwitch();
        }
        // 是否打开震动
        if (settingsModel.getSettingMsgVibrate()){
            switch_shake.openSwitch();
        } else {
            switch_shake.closeSwitch();
        }
        // 是否打开扬声器
        if (settingsModel.getSettingMsgSpeaker()){
            switch_speaker.openSwitch();
        } else {
            switch_speaker.closeSwitch();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_out:  //退出登录
                logout();
                break;
            case R.id.ll_switch_notification:
                if (switch_notification.isSwitchOpen()){
                    switch_notification.closeSwitch();
                    ll_switch_sound.setVisibility(View.GONE);
                    ll_switch_shake.setVisibility(View.GONE);

                    settingsModel.setSettingMsgNotification(false);

                } else {
                    switch_notification.openSwitch();
                    ll_switch_sound.setVisibility(View.VISIBLE);
                    ll_switch_shake.setVisibility(View.VISIBLE);

                    settingsModel.setSettingMsgNotification(true);
                }
                break;
            case R.id.ll_switch_sound:
                if (switch_sound.isSwitchOpen()){
                    switch_sound.closeSwitch();
                    settingsModel.setSettingMsgSound(false);
                } else {
                    switch_sound.openSwitch();
                    settingsModel.setSettingMsgSound(true);
                }
                break;
            case R.id.ll_switch_shake:
                if (switch_shake.isSwitchOpen()){
                    switch_shake.closeSwitch();
                    settingsModel.setSettingMsgVibrate(false);
                } else {
                    switch_shake.openSwitch();
                    settingsModel.setSettingMsgVibrate(true);
                }
                break;
            case R.id.ll_switch_speaker:
                if (switch_speaker.isSwitchOpen()){
                    switch_speaker.closeSwitch();
                    settingsModel.setSettingMsgSpeaker(false);
                } else {
                    switch_speaker.openSwitch();
                    settingsModel.setSettingMsgSpeaker(true);
                }
                break;
            case R.id.ll_black_list:
                startActivity(new Intent(getActivity(), BlackListActivity.class));
                break;
        }
    }

     void logout() {
         final ProgressDialog pd = new ProgressDialog(getActivity());
         String st = getResources().getString(R.string.Are_logged_out);
         pd.setMessage(st);
         pd.setCanceledOnTouchOutside(false);
         pd.show();
         RenRenXinHelper.getInstance().logout(false, new EMCallBack() {
             @Override
             public void onSuccess() {
                 getActivity().runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         pd.dismiss();
                         //重新显示登录页面
                         ((MainActivity) getActivity()).finish();
                         startActivity(new Intent(getActivity(), LoginActivity.class));
                     }
                 });
             }

             @Override
             public void onError(int i, String s) {
                 getActivity().runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         pd.dismiss();
                         Toast.makeText(getActivity(), "退出登录失败！", Toast.LENGTH_SHORT).show();
                     }
                 });
             }

             @Override
             public void onProgress(int i, String s) {

             }
         });
    }
}
